using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using AppPriceLive.Entities.Models;
using AppPriceLive.Entities.Utils;
using AppPriceLive.Common;

namespace AppPriceLive.DAL.Base
{
	public class CurrencyGroupDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public CurrencyGroupDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the CurrencyGroup table.
		/// </summary>
		public virtual void Insert(CurrencyGroupInfo currencyGroupInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@VName", currencyGroupInfo.VName),
				new SqlParameter("@EName", currencyGroupInfo.EName),
				new SqlParameter("@OrderBy", currencyGroupInfo.OrderBy)
			};

			currencyGroupInfo.CurrencyGroupId = (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "CurrencyGroup_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the CurrencyGroup table.
		/// </summary>
		public virtual void Update(CurrencyGroupInfo currencyGroupInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CurrencyGroupId", currencyGroupInfo.CurrencyGroupId),
				new SqlParameter("@VName", currencyGroupInfo.VName),
				new SqlParameter("@EName", currencyGroupInfo.EName),
				new SqlParameter("@OrderBy", currencyGroupInfo.OrderBy)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CurrencyGroup_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the CurrencyGroup table by its primary key.
		/// </summary>
		public virtual void Delete(int? currencyGroupId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CurrencyGroupId", currencyGroupId)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CurrencyGroup_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the CurrencyGroup table.
		/// </summary>
		public virtual CurrencyGroupInfo Select(int? currencyGroupId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CurrencyGroupId", currencyGroupId)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CurrencyGroup_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeCurrencyGroupInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the CurrencyGroup table.
		/// </summary>
		public List<CurrencyGroupInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CurrencyGroup_SelectAll"))
			{
				List<CurrencyGroupInfo> currencyGroupInfoList = new List<CurrencyGroupInfo>();
				while (dataReader.Read())
				{
					currencyGroupInfoList.Add(MakeCurrencyGroupInfo(dataReader));
				}

				return currencyGroupInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the CurrencyGroup class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual CurrencyGroupInfo MakeCurrencyGroupInfo(SqlDataReader dataReader)
		{
			CurrencyGroupInfo currencyGroupInfo = new CurrencyGroupInfo();
			currencyGroupInfo.CurrencyGroupId = SqlClientUtility.GetInt32(dataReader, DbConstants.CURRENCYGROUP.CURRENCYGROUPID);
			currencyGroupInfo.VName = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYGROUP.VNAME);
			currencyGroupInfo.EName = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYGROUP.ENAME);
			currencyGroupInfo.OrderBy = SqlClientUtility.GetInt32(dataReader, DbConstants.CURRENCYGROUP.ORDERBY);

			return currencyGroupInfo;
		}

		#endregion
	}
}
